﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Mvc;
using ArticleWeb.Models;

namespace ArticleWeb.Controllers
{
    [Authorize]
    public class ArticleTagsController : Controller
    {
        private ApplicationDbContext db = new ApplicationDbContext();

        // GET: ArticleTags
        public ActionResult Index(int page = 1)
        {
            var query = db.ArticleTags.OrderBy(x => x.ID);
            ViewBag.Total = query.Count();
            return View(query.ToList(page));
        }

        // GET: ArticleTags/Details/5
        public ActionResult Details(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            ArticleTag articleTag = db.ArticleTags.Find(id);
            if (articleTag == null)
            {
                return HttpNotFound();
            }
            return View(articleTag);
        }

        public ActionResult Exists(int? id, string newName)
        {
            if (string.IsNullOrEmpty(newName))
            {
                return this.JsonError("标签名称不能为空");
            }
            if (id == null)//when create new
            {
                if (db.ArticleTags.Any(x => x.TagText == newName))
                {
                    return this.JsonDuplicate();
                }
                else
                    return this.JsonOk();
            }
            else // when modify name
            {
                if (db.ArticleTags.Any(x => x.TagText == newName && x.ID != id))
                {
                    return this.JsonDuplicate();
                }
                else
                    return this.JsonOk();
            }
        }

        // GET: ArticleTags/Create
        public ActionResult Create()
        {
            return View();
        }

        // POST: ArticleTags/Create
        // To protect from overposting attacks, please enable the specific properties you want to bind to, for 
        // more details see http://go.microsoft.com/fwlink/?LinkId=317598.
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create([Bind(Include = "ID,TagText,Fragments,Referenced,CreateTime,UpdateTime")] ArticleTag articleTag)
        {
            if (ModelState.IsValid)
            {
                db.ArticleTags.Add(articleTag);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            return View(articleTag);
        }

        // GET: ArticleTags/Edit/5
        public ActionResult Edit(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            ArticleTag articleTag = db.ArticleTags.Find(id);
            if (articleTag == null)
            {
                return HttpNotFound();
            }
            return View(articleTag);
        }

        // POST: ArticleTags/Edit/5
        // To protect from overposting attacks, please enable the specific properties you want to bind to, for 
        // more details see http://go.microsoft.com/fwlink/?LinkId=317598.
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Edit([Bind(Include = "ID,TagText")] ArticleTag articleTag)
        {
            if (ModelState.IsValid)
            {
                var m = db.ArticleTags.Find(articleTag.ID);
                m.TagText = articleTag.TagText;
                m.UpdateTime = DateTime.Now;
                db.Entry(m).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(articleTag);
        }

        // GET: ArticleTags/Delete/5
        public ActionResult Delete(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            ArticleTag articleTag = db.ArticleTags.Find(id);
            if (articleTag == null)
            {
                return HttpNotFound();
            }
            return View(articleTag);
        }

        // POST: ArticleTags/Delete/5
        [HttpPost, ActionName("Delete")]
        [ValidateAntiForgeryToken]
        public ActionResult DeleteConfirmed(int id)
        {
            ArticleTag articleTag = db.ArticleTags.Find(id);
            db.ArticleTags.Remove(articleTag);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                db.Dispose();
            }
            base.Dispose(disposing);
        }
    }
}
